//
//  XZ_PayView.m
//  Project_Demo
//
//  Created by Leon on 2018/7/19.
//  Copyright © 2018年 caoxiaozhu. All rights reserved.
//

#import "XZ_PayView.h"

@interface XZ_PayView()<UITextFieldDelegate>
@property(nonatomic,strong)UIView *middleChooseBtnView;
@property(nonatomic,strong)UIButton *selectedBtn;
@property(nonatomic,strong)UIImageView *chooseImageView;
@property(nonatomic,assign)NSInteger amount;
@property(nonatomic,assign)NSInteger payWayTag;
@property(nonatomic,strong)UITextField *otherAmountField;
@property(nonatomic,strong)UITextField *cardNoField;
@end

@implementation XZ_PayView

SINGLETON_FOR_CLASS(XZ_PayView);

-(void)layoutSubviews{
    [super layoutSubviews];
    [self setupUI];
}

-(void)setupUI{
    //mainScrollView
    UIScrollView *mainScrollView = [[UIScrollView alloc]init];
    mainScrollView.bounces = false;
    [mainScrollView setOrigin:CGPointMake(0, 0)];
    [mainScrollView setSize:CGSizeMake(kScreenWidth, kScreenHeight)];
    mainScrollView.showsVerticalScrollIndicator = false;
    [self addSubview:mainScrollView];
    
    //最上方的输入框
    UIView *inputBacView= [UIView createTopBottomLineViewWithBacgroundColor:Color_White Frame:CGRectMake(0, 5, kScreenWidth, percentageScreenHeight * 3/4)];
    [mainScrollView addSubview:inputBacView];
    
    //卡号label
    UILabel *cardLabel = [UILabel labelWithStr:@"卡号" Frame:CGRectMake(10, 0, 50, inputBacView.height) fontSize:FONT(15) textLine:0 textColor:Color_Black TextAlignment:NSTextAlignmentCenter];
    cardLabel.text = @"卡号";
    cardLabel.centerY = inputBacView.centerY - 5;
    [inputBacView addSubview:cardLabel];
    
    //inputField
    _cardNoField = [UITextField textFieldWithPlaceHolder:@"请输入卡号" PlaceHolderColor:nil BorderType:UITextBorderStyleNone SecureTextEntry:false Text:nil TextColor:Color_Black BackgroundColor:Color_Clear KeyboardType:UIKeyboardTypeNumberPad];
    _cardNoField.frame = CGRectMake(cardLabel.right+5, 0, percentageScreenWidth * 6 , inputBacView.height);
    [inputBacView addSubview:_cardNoField];
    
    //充值金额
    UILabel *amountTagLabel = [UILabel labelWithStr:@"充值金额" Frame:CGRectMake(10, inputBacView.bottom+5, kScreenWidth, 30) fontSize:FONT(13) textLine:0 textColor:Color_Gray TextAlignment:NSTextAlignmentLeft];
    [mainScrollView addSubview:amountTagLabel];
    
    //中间的按钮
    _middleChooseBtnView = [UIView createTopLineViewWithBacgroundColor:Color_White Frame:CGRectMake(0, amountTagLabel.bottom +5, kScreenWidth, percentageScreenHeight *1.5)];
    [mainScrollView addSubview:_middleChooseBtnView];
    
    NSInteger count = 3;
    CGFloat margin = 10;
    CGFloat btnW = (kScreenWidth - (count + 1) * margin) / count;
    CGFloat btnH = 40;
    for (int i = 0 ; i<_amountTitleArr.count; i++) {
        NSInteger row = i / count;
        NSInteger col = i % count;
        UIButton *chooseBtn = [UIButton createButtonWithFrame:CGRectMake(margin + (btnW + margin) * col, margin +(btnH + margin) *row, btnW, btnH) buttonType:kButtonNormal title:_amountTitleArr[i] tag:i target:self action:@selector(clickToChoose:)];
        [chooseBtn setTitleColor:Color_Red forState:UIControlStateSelected];
        [chooseBtn setBackgroundColor:Color_White forState:UIControlStateNormal];
        [_middleChooseBtnView addSubview:chooseBtn];
        
        //默认第一个选中
        if (i==0) {
            chooseBtn.selectedRed = true;
            _amount = [chooseBtn.titleLabel.text trimOnlyNum];
        }
        
        if (i==_amountTitleArr.count-1) {
            _otherAmountField = [UITextField textFieldWithPlaceHolder:nil PlaceHolderColor:nil BorderType:UITextBorderStyleNone SecureTextEntry:false Text:@"其他金额" TextColor:Color_Black BackgroundColor:nil KeyboardType:UIKeyboardTypeNumberPad];
            _otherAmountField.frame = CGRectMake(0, 0, chooseBtn.width, chooseBtn.height);
            _otherAmountField.delegate = self;
            _otherAmountField.clearButtonMode = UITextFieldViewModeNever;
            _otherAmountField.textAlignment = NSTextAlignmentCenter;
            [chooseBtn addSubview:_otherAmountField];
            _middleChooseBtnView.height = ((row+1) *btnH + (row+2)* margin);
            _middleChooseBtnView.addBottomLineView = true;
        }
    }
    
    //支付方式
    //充值金额
    UILabel *payWayTitleLabel = [UILabel labelWithStr:@"支付方式" Frame:CGRectMake(10, _middleChooseBtnView.bottom +5, kScreenWidth, 30) fontSize:FONT(13) textLine:0 textColor:Color_Gray TextAlignment:NSTextAlignmentLeft];
    [mainScrollView addSubview:payWayTitleLabel];
    
    //支付方式View
    UIView *payWayBacView = [[UIView alloc]initWithFrame:CGRectMake(0, payWayTitleLabel.bottom+5, kScreenWidth, 100)];
    payWayBacView.backgroundColor = Color_White;
    UIView *topLineView = [UIView lineViewWithTop:true Position:payWayBacView.bounds.origin.y Color:nil];
    [payWayBacView addSubview:topLineView];
    [mainScrollView addSubview:payWayBacView];
    
    //创建支付Btn
    CGFloat payBtnH = 60;
    CGFloat payBtnW = kScreenWidth;
    for (int i = 0; i<_payWayImageArr.count; i++) {
        CGFloat maxY = i *payBtnH;
        UIButton *payWayBtn = [[UIButton alloc]init];
//        payWayBtn.backgroundColor = Color_Random;
        payWayBtn.frame = CGRectMake(15, maxY, payBtnW, payBtnH);
        [payWayBtn setImage:[UIImage imageNamed:_payWayImageArr[i]] forState:UIControlStateNormal];
        [payWayBtn addTarget:self action:@selector(clickToChoosePayWay:) forControlEvents:UIControlEventTouchUpInside];
        payWayBtn.tag = i;
        payWayBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [payWayBtn setTitle:_payWayTitleArr[i] forState:UIControlStateNormal];
        [payWayBtn setImagePosition:LXMImagePositionLeft spacing:15];
        [payWayBtn setTitleColor:Color_Black forState:UIControlStateNormal];
        [payWayBacView addSubview:payWayBtn];
        
        if (i==0) {
            _chooseImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"选择"]];
            _chooseImageView.frame = CGRectMake(percentageScreenWidth *6.8, 0, 20, 20);
            _chooseImageView.centerY = payWayBtn.centerY;
            _payWayTag = i;
            [payWayBtn addSubview:_chooseImageView];
        }
        
        //添加中间的分割线
        if (i != _payWayImageArr.count-1) {
            UIView *separateLineView = [UIView separateBottomLineWithColor:nil];
            [separateLineView setOrigin:CGPointMake(15, payWayBtn.height)];
            [payWayBtn addSubview:separateLineView];
        }
        
        //添加尾部的lineView
        payWayBacView.height = _payWayImageArr.count * payBtnH;
        UIView *bottomLineView = [UIView lineViewWithTop:false Position:payWayBacView.height Color:nil];
        [payWayBacView addSubview:bottomLineView];
    }
    
    //底部的立即充值按钮
    UIButton *goToPayButton = [[UIButton alloc]init];
    [goToPayButton setTitle:@"立即充值" forState:UIControlStateNormal];
    [goToPayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    goToPayButton.backgroundColor = [UIColor hex:@"0f6ab5"];
    goToPayButton.frame = CGRectMake(15, payWayBacView.bottom+15, kScreenWidth-30, 50);
    [mainScrollView addSubview:goToPayButton];
    
    [goToPayButton addTarget:self action:@selector(clickToGoPay) forControlEvents:UIControlEventTouchUpInside];
    
    //温馨提示label
    //
    NSString *noticeStr = _bottomNoticeLabelStr;
    CGFloat strHeight = [noticeStr heightWithFontSize:15 width:kScreenWidth-30];
    UILabel *noticeLabel = [UILabel labelWithStr:noticeStr Frame:CGRectMake(15, goToPayButton.bottom + 5, kScreenWidth-30, strHeight) fontSize:FONT(15) textLine:0 textColor:Color_Black TextAlignment:NSTextAlignmentLeft];
    [mainScrollView addSubview:noticeLabel];
//    NSLog(@"bottomHieght==>%f",noticeLabel.bottom);
    
    mainScrollView.contentSize = CGSizeMake(0, noticeLabel.bottom + naviHeight+percentageScreenHeight);
    
}


//中间选择金额按钮
-(void)clickToChoose:(UIButton *)button{
    [self endEditing:YES];
    button.selectedRed = true;
    [UIView allBtnDeSelected:_middleChooseBtnView onlyOneSelected:button];
    //分离出金额
    _amount = [button.titleLabel.text trimOnlyNum];
}

//下面选择支付方式
-(void)clickToChoosePayWay:(UIButton *)button{
//    NSLog(@"选择了");
    _chooseImageView.centerY = button.centerY;
    _payWayTag = button.tag;
}

//返回参数
-(void)clickToGoPay{
//    _backBlock(_payWayTag, _amount);
    [self endEditing:true];
    
    if ([_cardNoField.text isEmpty]) {
        [XZ_SVProgressView showWithDescription:@"请输入卡号" showType:ShowInfo];
        return;
    }
    
    [_delegate payAmount:_amount Payway:_payWayTag CardNo:_cardNoField.text];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    textField.text = @"";
    [UIView allBtnDeSelected:_middleChooseBtnView onlyOneSelectedWithTag:5];
    return true;
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    if ([textField.text isEmpty]) {
        textField.text = @"其他金额";
    }
    
    [UIView allBtnDeSelected:_middleChooseBtnView];
    //分离出金额
    _amount = [textField.text integerValue];
    return true;
}

@end
